package com.example.myapplication3;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication3.R;

public class SaveActivity extends AppCompatActivity {

    private static final String TAG = "SaveActivity";
    private EditText inpdollar;
    private EditText inppound;
    private EditText inpeuro;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_save);
        Intent intent = getIntent();
        float dollar2 = intent.getFloatExtra("dollar_rate_key", 0.0f);
        float pound2 = intent.getFloatExtra("pound_rate_key", 0.0f);
        float euro2 = intent.getFloatExtra("euro_rate_key", 0.0f);
        Log.i(TAG, "onCreate:dollar2=" + dollar2);
        Log.i(TAG, "onCreate:pound2=" + pound2);
        Log.i(TAG, "onCreate:euro2=" + euro2);

        //获取控件
        inpdollar = findViewById(R.id.editTextTextnumber);
        inppound = findViewById(R.id.editTextTextnumber2);
        inpeuro = findViewById(R.id.editTextTextnumber3);

        //将汇率值放入控件
        inpdollar.setText(String.valueOf(dollar2));
        inppound.setText(String.valueOf(pound2));
        inpeuro.setText(String.valueOf(euro2));
    }

    public void save(View btn) {
        //获得输入的数据
        float newdollar = Float.parseFloat(inpdollar.getText().toString());
        float newpound = Float.parseFloat(inppound.getText().toString());
        float neweuro = Float.parseFloat(inpeuro.getText().toString());
        //将输入的数据带回计算页面
        Intent intent = getIntent();
        Bundle bdl = new Bundle();
        bdl.putFloat("key_dollar", newdollar);
        bdl.putFloat("key_pound", newpound);
        bdl.putFloat("key_euro", neweuro);
        intent.putExtras(bdl);
        setResult(2, intent);//设置resultCode及带回的数据
        // 如果使用Intent config = new Intent(this,RateActivity.class);则运行一次会创建一个新页面，电脑会崩溃
        //返回到调用页面
        finish(); //如果是个被调用的窗口，则返回上个窗口，若不是，则结束程序
    }
}